*Insheet and process JOLTS researcher data
cd /OEUS/users/FEDSusers/JOLTS/
set more off
use ./data/JOLTS_ntilde, clear

gen gact3 = 200*(emp - L3.emp)/(emp + L3.emp) if emp>0 & L3.emp>0 & emp!=. & L3.emp!=.
*Calculate inaction windows 
foreach i of numlist 3{
	gen inactalt_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 
	replace inactalt_`i' = 1 if emp == ntilde`i'  & inactalt_`i' == 0
	
	gen inactaltposh_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 & hires`i'>0 & hires`i'!=.
	replace inactaltposh_`i' = 1 if emp == ntilde`i'  & inactaltposh_`i' == 0 
	
	gen inact_`i' = 0 if emp!=. & L3.emp!=. & emp>0 & L3.emp>0
	replace inact_`i' = 1 if emp==L3.emp & emp>0 & L3.emp>0 & emp!=. & L3.emp!=.
}

*Define quarterly inaction with a window as maximum of emp and percentage
*max(1,1%)

foreach i of numlist 3{
	gen inactalt11_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 
	replace inactalt11_`i' = 1 if emp >= min(floor(.99*ntilde`i'),ntilde`i'-1) & emp<=max(ceil(1.01*ntilde`i'),ntilde`i'+1) & inactalt11_`i' == 0
	
	gen inactalt11posh_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 & hires`i'>0 & hires`i'!=.	
	replace inactalt11posh_`i'= 1 if emp >= min(floor(.99*ntilde`i'),ntilde`i'-1) & emp<=max(ceil(1.01*ntilde`i'),ntilde`i'+1) & inactalt11posh_`i' == 0  
	
}

*max(2,2%)
foreach i of numlist 3{
	gen inactalt22_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 
	replace inactalt22_`i' = 1 if emp >= min(floor(.98*ntilde`i'),ntilde`i'-2) & emp<=max(ceil(1.02*ntilde`i'),ntilde`i'+2) & inactalt22_`i' == 0 
	
	gen inactalt22posh_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0  & hires`i'>0 & hires`i'!=.	
	replace inactalt22posh_`i' = 1 if emp >= min(floor(.98*ntilde`i'),ntilde`i'-2) & emp<=max(ceil(1.02*ntilde`i'),ntilde`i'+2) & inactalt22posh_`i' == 0 
}


*Keep only quarter ends
gen month=month(dofm(mdate))
keep if month==3|month==6|month==9|month==12
replace g3 = g3*100

gen gdist = 0 if inactalt_3==1 & emp>0 & emp!=. & ntilde3>0 & ntilde3!=.
gen gdist_g20 =0 if inactalt_3==1 & emp>0 & ntilde3>20 & emp!=. & ntilde3!=.
gen gdist_posh = 0 if inactaltposh_3==1 & emp>0 & ntilde3>0 & emp!=. & ntilde3!=. & hires3>0 & hires3!=.
gen gdistact = 0 if inact_3==1 & emp>0 & L3.emp>0 & emp!=. & L3.emp!=.

gen bin = 0 if emp>0 & emp!=. & ntilde3>0 & ntilde3!=.
gen bin_g20 = 0 if emp>0 & ntilde3>20 & emp!=. & ntilde3!=.
gen bin_posh = 0 if emp>0 & ntilde3>0 & emp!=. & ntilde3!=. & hires3>0 & hires3!=.
gen binact = 0 if emp>0 & L3.emp>0 & emp!=. & L3.emp!=.

forvalues x = 1/80{
replace gdist = `x' if g3>2.5*(`x'-1) & g3<=2.5*(`x') & emp>0 & emp!=. & ntilde3>0 & ntilde3!=.
replace gdist_g20 = `x' if g3>2.5*(`x'-1) & g3<=2.5*(`x') & emp>0 & emp!=. & ntilde3>20 & ntilde3!=.
replace gdist_posh = `x' if g3>2.5*(`x'-1) & g3<=2.5*(`x') & emp>0 & emp!=. & ntilde3>0 & ntilde3!=. & hires3>0 & hires3!=.
replace gdistact = `x' if gact3>2.5*(`x'-1) & gact3<=2.5*(`x') & emp>0 & emp!=. & L3.emp>0 & L3.emp!=.
replace bin = `x'*2.5 if g3>2.5*(`x'-1) & g3<=2.5*(`x') & emp>0 & emp!=. & ntilde3>0 & ntilde3!=.
replace bin_g20 = `x'*2.5 if g3>2.5*(`x'-1) & g3<=2.5*(`x') & emp>0 & emp!=. & ntilde3>20 & ntilde3!=.
replace bin_posh = `x'*2.5 if g3>2.5*(`x'-1) & g3<=2.5*(`x') & emp>0 & emp!=. & ntilde3>0 & ntilde3!=. & hires3>0 & hires3!=.
replace binact = `x'*2.5 if gact3>2.5*(`x'-1) & gact3<=2.5*(`x') & emp>0 & emp!=. & L3.emp>0 & L3.emp!=.
}
forvalues x = 1(1)80{
replace gdist = -`x' if g3>=-2.5*(`x') & g3<-2.5*(`x'-1) & emp>0 & emp!=. & ntilde3>0 & ntilde3!=.
replace gdist_g20 = -`x' if g3>=-2.5*(`x') & g3<-2.5*(`x'-1) & emp>0 & emp!=. & ntilde3>20 & ntilde3!=.
replace gdist_posh = -`x' if g3>=-2.5*(`x') & g3<-2.5*(`x'-1) & emp>0 & emp!=. & ntilde3>0 & ntilde3!=. & hires3>0 & hires3!=.
replace gdistact = -`x' if gact3>=-2.5*(`x') & gact3<-2.5*(`x'-1) & emp>0 & emp!=. & L3.emp>0 & L3.emp!=.
replace bin = -`x'*2.5 if g3>=-2.5*(`x') & g3<-2.5*(`x'-1) & emp>0 & emp!=. & ntilde3>0 & ntilde3!=.
replace bin_g20 =  -`x'*2.5  if g3>=-2.5*(`x') & g3<-2.5*(`x'-1) & emp>0 & emp!=. & ntilde3>20 & ntilde3!=.
replace bin_posh = -`x'*2.5 if g3>=-2.5*(`x') & g3<-2.5*(`x'-1) & emp>0 & emp!=. & ntilde3>0 & ntilde3!=. & hires3>0 & hires3!=.
replace binact =  -`x'*2.5 if gact3>=-2.5*(`x') & gact3<-2.5*(`x'-1) & emp>0 & emp!=. & L3.emp>0 & L3.emp!=.
}

*Shuffle cells when inaction11==1 and inaction22==1
gen gdist11 = gdist
replace gdist11 = 0 if inactalt11_3==1 & emp>0 & emp!=. & ntilde3>0 & ntilde3!=.
gen bin11 = bin
replace bin11 = 0 if gdist11==0

gen gdist22 = gdist 
replace gdist22 = 0 if inactalt22_3==1  & emp>0 & emp!=. & ntilde3>0 & ntilde3!=.
gen bin22 = bin
replace bin22 = 0 if gdist22 == 0

gen gdist11_g20 = gdist_g20
replace gdist11_g20 = 0 if inactalt11_3==1 & emp>0 & emp!=. & ntilde3>20 & ntilde3!=.
gen bin11_g20 = bin_g20
replace bin11_g20 = 0 if gdist11_g20==0

gen gdist22_g20 = gdist_g20
replace gdist22_g20 = 0 if inactalt22_3==1 & emp>0 & emp!=. & ntilde3>20 & ntilde3!=.
gen bin22_g20 = bin_g20
replace bin22_g20 = 0 if gdist22_g20==0

gen gdist11_posh = gdist_posh
replace gdist11_posh = 0 if inactalt11posh_3==1 & emp>0 & emp!=. & ntilde3>0 & ntilde3!=. & hires3>0 & hires3!=.
gen bin11_posh = bin_posh
replace bin11_posh = 0 if gdist11_posh==0

gen gdist22_posh = gdist_posh
replace gdist22_posh = 0 if inactalt22posh_3==1 & emp>0 & emp!=. & ntilde3>0 & ntilde3!=. & hires3>0 & hires3!=.
gen bin22_posh = bin_posh
replace bin22_posh = 0 if gdist22_posh==0

foreach x in "" 11 22{
	foreach y in "" _g20 _posh{
		preserve
		collapse (rawsum) weight_f (mean) bin`x'`y' (count) N`x'=weight_f, by(gdist`x'`y')
drop if gdist`x'`y' ==. 
egen csum = sum(weight_f)
gen dist`x' = weight_f/csum
replace dist`x' = . if N`x'<100
rename bin`x'`y' bin
keep bin dist`x' N`x'
save growthdists`x'`y'_rnd2final, replace
restore
		}
	}	

collapse (rawsum) weight_f (mean) binact (count) N=weight_f, by(gdistact)
drop if gdistact==. 
egen csum = sum(weight_f)
gen dist = weight_f/csum
replace dist = . if N<100
rename binact bin
keep bin dist N
save ./rnd2final/out_growthdists_act_rnd2final, replace
	
foreach y in "" _g20 _posh{
	use growthdists`y'_rnd2final, clear
		foreach x in 11 22{
	merge 1:1 bin using growthdists`x'`y'_rnd2final, nogen
	save out_growthdists`y'_20240416, replace
	outsheet using ./rnd2final/out_growthdists`y'_rnd2final.csv, replace c

	}
}
	!rm growthdists*
